<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Superadmins\Controller;

/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class ArticleController extends CommonController {
	public function index(){
		$pagesize=6;
		$get_data=I('get.');
		$map=$this->getWhere($get_data);
		$count      = $this->getArticleCount($map);
		$Page       = new PageController($count,$pagesize);
		$document_data=$this->showSchoolArticle($map,&$Page);
		$this->assign("document_data",$document_data);
		$show       = $Page->show();			// 分页显示输出
		$this->assign('page',$show);			// 赋值分页输出
		$this->assign('get_data',$get_data);	// 赋值分页输出
		$this->display();
	}
	
	//动态获取学校列表
	function getSchoolName(){
		$School=D('School');
		$map['school_name']=array('like','%'.I('get.school_name').'%');
		$School_data=$School->where($map)->limit('0,5')->getField('school_id,school_name');
		$this->ajaxReturn($School_data);
	}
	
	function getWhere($get_data){
		$cateaids=array(				//类别与category id对应
			'Brochure'	=>	'50,54,58',		//招生简章
			'Policy'	=>	'49,53,57',		//招生政策
			'Plan'		=>	'51,55,59',		//招生计划
			'News'		=>	'2'				//院校新闻
		);
		$Brochure=array(
			'GZ'	=>	'54',
			'YZ'	=>	'50',
			'YK'	=>	'58'
		);
		$Policy=array(
			'GZ'	=>	'53',
			'YZ'	=>	'49',
			'YK'	=>	'57'
		);
		$Plan=array(
			'GZ'	=>	'55',
			'YZ'	=>	'51',
			'YK'	=>	'59'
		);
		
		if(isset($get_data)){
			if($get_data['school_name']){				//学校名称
				$map['school_name']=array('like','%'.$get_data['school_name'].'%');
				if($get_data['school_id']){
					$map['school_id']=$get_data['school_id'];
				}
			}
			
			if($get_data['title']){		
				$map['title']=array('like','%'.$get_data['title'].'%');					
			}
			
			if($get_data['time']){	
				$nowtime = time();	//strtotime("+1 week 3 days 7 hours 5 seconds")
				$start_time = 0;
				if($get_data['time']=='1'){
					$start_time = strtotime('-1 days');
				}else if($get_data['time']=='3'){
					$start_time = strtotime('-3 days');
				}else if($get_data['time']=='7'){
					$start_time = strtotime('-1 week');
				}else if($get_data['time']=='30'){
					$start_time = strtotime('-1 month');
				}else if($get_data['time']=='365'){
					$start_time = strtotime('-1 year');
				}
				$map['create_time']	= array('between',$start_time.','.$nowtime);
			}
			if($get_data['category']){	
				if($get_data['type']=='Brochure'){
					$map['category_id']=$Brochure[$get_data['category']];		
				}else if($get_data['type']=='Policy'){
					$map['category_id']=$Policy[$get_data['category']];		
				}else if($get_data['type']=='Plan'){
					$map['category_id']=$Plan[$get_data['category']];		
				}else if($get_data['type']=='News'){
					$map['category_id']=$cateaids['News'];
				}	
			}else{
				$map['category_id']=array('in',$cateaids[$get_data['type']]);
			}
			
		}
		$map['status']='1';
		return $map;
	}
	
	function getArticleCount($map){
		$document=D('Document');
		$count=$document->where($map)->count(); 
		return $count;
	}
	
	public function showSchoolArticle($map,$Page)
	{
		$document=D('Document');
		$document_data=$document->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('create_time desc')->select(); 
		return $document_data;
		
	}
/**
	 * 新增学校各项类别信息
	 * 
	 * */
	function addArticle(){	
		$post_data=I('post.');
		$map=$this->getWhere($post_data);
		$params['category']=$post_data['category'];
		$params['type']=$post_data['type'];
		$params['title']=trim($post_data['title']);
		$params['school_name']=$post_data['school_name'];
		if($post_data['school_id'] && $map['category_id'])
		{	
				$document=M('document');
				$article=M('document_article');	
				$data = array(										//document表更新数据
					'school_id'		=> $post_data['school_id'],		//学校ID
					'title'			=> $post_data['title'],			//文档标题
					'description'	=> $post_data['description'],	//文档描述
					'category_id'	=> $map['category_id'],	//文档分类ID
					//'model_id'		=>I('post.model_id'),	 	//内容模型ID
					//'type'			=>I('post.type'),	 		//内容类型ID
					//'link_id'		=> I('post.link_id'),	 		//链接ID
					//'level'			=> I('post.level'),	 		//级别
					'create_time'	=> time(),						//创建时间
					//'update_time'	=> time(),						//更新时间
					'pid'			=> 0,							//子分类ID
					'status'		=> 1,
				);
				$article_id=$document->add($data);
				if($article_id)
				{
					$contentdata = array(		
						'id'		=> $article_id,
						//'parse'		=> I('post.parse'),		//解析类型
						'content'	=> $post_data['content'],		//文章内容
						//'template'	=> I('post.template'),		//详情页显示模板
						//'bookmark'	=> I('post.bookmark'),		//文档收藏数
					);
					$articleflag=$article->add($contentdata);
					/*if($articleflag)
					{
						$this->error('“'.I('post.title').'” 新增失败！');
					}*/
				}
			
		}
		$this->redirect('Superadmins/Article/index',$params, 0, '页面跳转中...');
	}
	
/**
	 *编辑ajax获取信息内容 
	 */
	public function ajaxArticle(){
		$id=I('post.id');
		$data=$this->getArticleById($id);
		$title=$data['title'];
		$this->ajaxReturn($data);
	}
	function getArticleById($id){
		if($id){
			$document=D('Document');
			$map['id']=$id;
			$article_data=$document->where($map)->find();
			return $article_data;
		}
	}
	/**
	 *更新学校各项类别信息
	 * 
	 * */
	function updateArticle()
	{
           
		$data_post=I('post.');
		$params['type']=$data_post['type'];
		$params['title']=trim($data_post['title']);
		$params['school_name']=$data_post['school_name'];
		$document=M('document');
		$article=M('document_article');
		if(!empty($data_post['school_id']) && !empty($data_post['article_id']))
		{
			$where['id']=$data_post['article_id'];
			$article_where['id']=$data_post['article_id'];
			$data = array(									//document表更新数据
					//'school_id'		=> $data_post['school_id'],		//学校ID
					'title'			=> $data_post['title'],			//文档标题
					'description'	=> $data_post['description'],	//文档描述
					//'category_id'	=> $data_post['category_id'],	//文档分类ID
					//'model_id'	=> $data_post['model_id'],	 	//内容模型ID
					//'type'		=> $data_post['type'],	 		//内容类型ID
					//'link_id'		=> $data_post['link_id'],	 	//链接ID
					//'level'		=> $data_post['level'],	 		//级别
					//'create_time'	=> $data_post['create_time'],	//创建时间
					'update_time'	=> time(),	//更新时间
					//'status'		=> $data_post['status'],		//状态位
					//'pid'			=> $data_post['pid'],			//子分类ID
					//'status'		=> $data_post['status'],
				);
			$contentdata = array(		
						//'id'		=> $data_post['article_id'],
						//'parse'		=> I('post.parse'),		//解析类型
						'content'	=> I('post.content'),		//文章内容
						//'template'	=> I('post.template'),		//详情页显示模板
						//'bookmark'	=> I('post.bookmark'),		//文档收藏数
			);
			$flag=$document->where($where)->save($data);			//更新document表
			if($flag)
			{
				$article->where($article_where)->save($contentdata);	//更新document_article表
			}else{
				$this->error ('“'.$_POST['title'].'” 更新失败！');
			}
		}
		$this->redirect('Superadmins/Article/index',$params, 0, '页面跳转中...');
	}
	
	
	 /**
	  * 删除各项类别信息
	  *  假删除 更新状态位为-1
	  **/
	function deleteArticle($id)
	{
		$article_id=I('post.id');
		if(!empty($article_id) && $article_id>0)
		{
			$document=M('document');
			$where['id']=$article_id;
			$data=array
			(
				'status' => '-1'
			);
			if(!($document->where($where)->save($data)))
			{
				$this->error('删除失败！');
			}
		}
	}
	
}
